package com.tiantianyuegou.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.tiantianyuegou.R;
import com.tiantianyuegou.adapter.ProductSettingAdapter;
import com.tiantianyuegou.constant.Constant;
import com.tiantianyuegou.http.ApiDefine;
import com.tiantianyuegou.model.ProductSettingModel;
import com.tiantianyuegou.util.LoadingDialog;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * 商品分类(三级类型)
 * Created by ivany on 2016/1/20.
 */
public class ProductClassThreeActivity extends BaseActivity implements AdapterView.OnItemClickListener, View.OnClickListener {

    private ImageView mImg_back;
    private TextView mTxt_title, mTxt_menu;
    private ListView mListView;
    private ProductSettingAdapter mAdapter;
    private List<ProductSettingModel> mData;

    private LoadingDialog mLoadingDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_product_setting);
        initViews();
    }

    @Override
    public void findViewByIds() {
        mImg_back = (ImageView) findViewById(R.id.img_title);
        mTxt_title = (TextView) findViewById(R.id.text_title);
        mTxt_menu = (TextView) findViewById(R.id.text_menu);
        mListView = (ListView) findViewById(R.id.product_setting_list);
    }

    @Override
    public void initListener() {
        mListView.setOnItemClickListener(this);
        mImg_back.setOnClickListener(this);
    }

    @Override
    public void initData() {
        mLoadingDialog = new LoadingDialog(this);
        mTxt_title.setText("选择商品分类");
        mData = new ArrayList<>();
        mAdapter = new ProductSettingAdapter(this);
        mListView.setAdapter(mAdapter);
        getProductClass(getIntent().getStringExtra("id"));
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        ProductSettingModel model = mData.get(position);
        Intent intent = new Intent();
        Constant.productClassThreeId = model.getId();
        Constant.productClassThreeName = model.getLabel();
        setResult(Constant.GET_PRODUCT_CLASS_THREE_RESULT, intent);
        finish();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.img_title:
                finish();
                break;
        }
    }

    private void getProductClass(String id) {
        mLoadingDialog.setMsg("正在获取商品分类...");
        mLoadingDialog.show();
        RequestParams params = new RequestParams();
        params.addBodyParameter("parentId", id);
        loadData(HttpRequest.HttpMethod.POST, ApiDefine.CATEGORY, params, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                mLoadingDialog.dismiss();
                Log.e("TAG", "get product class response:" + responseInfo.result);
                if (TextUtils.isEmpty(responseInfo.result)) {
                    showMsg("获取商品分类列表失败");
                } else {
                    try {
                        JSONObject root = new JSONObject(responseInfo.result);
                        String code = root.getString("code");
                        if ("100".equals(code)) {
                            JSONArray info = root.getJSONArray("info");
                            if (info.length() > 0) {
                                mData.clear();
                                for (int i = 0; i < info.length(); i++) {
                                    JSONObject infoValue = info.getJSONObject(i);
                                    ProductSettingModel model = new ProductSettingModel();
                                    model.setId(infoValue.getInt("tpc_id"));
                                    model.setLabel(infoValue.getString("tpc_name"));
                                    model.setType("1");
                                    mData.add(model);
                                }
//                                TODO 需要修改
//                                mAdapter.setData(mData);
                                mAdapter.notifyDataSetChanged();
                            } else {
                                showMsg("暂无商品分类");
                            }
                        } else {
                            String msg = root.getString("message");
                            showMsg(msg);
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }

            @Override
            public void onFailure(HttpException e, String s) {
                mLoadingDialog.dismiss();
                Log.e("TAG", "get product class failure:" + s);
                showMsg("当前网络不可用,请检查您的网络设置");
            }
        });
    }
}
